//
//  BaseTableViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/24.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "BaseTableViewController.h"

@interface BaseTableViewController ()<UITableViewDelegate, UITableViewDataSource>


@end

@implementation BaseTableViewController

- (void)viewDidLoad{
    [super viewDidLoad];
    
    {
        self.isRefresh = NO;
        self.isLoadMore = NO;
        self.page = 1;

    }
    
    {
        [self.view addSubview:self.reloadBtn];
        [self.view addSubview:self.reloadLab];

        _reloadBtn.mj_x = (KScreenWidth-_reloadBtn.width)/2.0;
        _reloadBtn.centerY = KScreenHeight/2.0-TJ_NavBarHeight;
        _reloadLab.mj_y = _reloadBtn.bottom + 20;
    }

    {
        [self registerTableViewCell];
        [self.view addSubview:self.tableView];
    }
    
    {
        [self setupUI];
        [self setupLayout];
        [self initData];
    }
}

- (void)setupUI{
    
}

- (void)setupLayout{
    
}

- (void)initData{
    
}

//MARK: - 设置 无网、获取数据失败、数据为空 情况下刷新按钮图片和提示文字
- (void)setReloadBtnImageToError
{
    //设置无网、获取数据失败 的图片
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"loadingError"] forState:UIControlStateNormal];
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"loadingError"] forState:UIControlStateHighlighted];
    [_reloadBtn setTitleEdgeInsets:UIEdgeInsetsMake([self fitSize:140], -[self fitSize:50], 0, -[self fitSize:50])];
    
    [_reloadBtn setTitle:@"咦？怎么木有网了呢~" forState:UIControlStateNormal];
}

- (void)setReloadBtnImageToEmpty:(NSString *)emptyText
{
    //设置 数据为空 情况下的图片
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"loadingEmpty"] forState:UIControlStateNormal];
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"loadingEmpty"] forState:UIControlStateHighlighted];
    [_reloadBtn setTitleEdgeInsets:UIEdgeInsetsMake([self fitSize:165], -[self fitSize:50], 0, -[self fitSize:50])];
    
    if (emptyText && emptyText.length > 0) {
        [_reloadBtn setTitle:emptyText forState:UIControlStateNormal];
    }else {
        [_reloadBtn setTitle:@"暂无数据" forState:UIControlStateNormal];
    }
}

- (UIButton *)reloadBtn{
    
    if (!_reloadBtn) {
        
        _reloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _reloadBtn.frame = CGRectMake(0, 0, 158, 132);
        [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"meat_tranfer_blank"] forState:UIControlStateNormal];
        [_reloadBtn addTarget:self action:@selector(refresh) forControlEvents:(UIControlEventTouchUpInside)];
        _reloadBtn.hidden = YES;
    }
    return _reloadBtn;
}


- (UILabel *)reloadLab{
    
    if (!_reloadLab) {
        _reloadLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0xB0B0B0) font:mFont(13) textAliment:1];
        _reloadLab.numberOfLines = 0;
        _reloadLab.size = CGSizeMake(200, 36);
        _reloadLab.mj_x = (KScreenWidth - _reloadLab.width)/2.0;
        
    }
    return _reloadLab;
}


#pragma mark - private method
//局部更新TableView
- (void)localReloadTableViewBySection:(NSInteger)section{
    kWeakSelf(self);
    [UIView performWithoutAnimation:^{
        NSIndexSet *reloadSet = [NSIndexSet indexSetWithIndex:section];
        [weakself.tableView reloadSections:reloadSet withRowAnimation:UITableViewRowAnimationNone];
    }];
}

//注册UItableViewCell
- (void)registerTableViewCell{
    
}


#pragma mark - tableview delegate/dataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0001;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return  [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UITableViewCell new];
}


#pragma mark - setter and getter

- (UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 0) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.separatorColor = KClearColor;
        _tableView.backgroundColor = mBackgroudColor;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        
        MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];

        // 设置文字
        [header setTitle:@"下拉刷新" forState:MJRefreshStateIdle];
        [header setTitle:@"松开刷新" forState:MJRefreshStatePulling];
        [header setTitle:@"刷新中" forState:MJRefreshStateRefreshing];
        
        header.stateLabel.font = mFont(12);
        header.stateLabel.textColor = UIColorFromRGB(0xB0B0B0);
        header.lastUpdatedTimeLabel.hidden = YES;
        self.tableView.mj_header = header;

        
        MJRefreshAutoNormalFooter *footer =  [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
        [footer setTitle:@"点击或者上拉加载更多" forState:MJRefreshStateIdle];
        [footer setTitle:@"加载更多" forState:MJRefreshStateRefreshing];
        [footer setTitle:@"我是有底线的" forState:MJRefreshStateNoMoreData];
        footer.stateLabel.font = mFont(13);
        footer.stateLabel.textColor = UIColorFromRGB(0xB0B0B0);
        
        self.tableView.mj_footer = footer;
        
    }
    return _tableView;
}


#pragma mark - 下拉/上拉

- (void)refresh
{
    self.isRefresh = YES;
}

- (void)loadMore
{
    self.page ++;
    self.isLoadMore = YES;
}

- (void)resetData
{
    self.page = 1;
    self.isLoadMore = NO;
}

- (void)endRefresh
{
    [self haveData];
    self.isRefresh = NO;
    [self.tableView.mj_header endRefreshing];
}

- (void)endLoadMore
{
    [self haveData];
    [self.tableView.mj_footer endRefreshing];
}

- (void)endLoadMoreMoData
{
    [self haveData];

    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    
    CGFloat center = KScreenWidth - 120;
    CGFloat width = 50;
    
    CAShapeLayer *line1 = [CAShapeLayer layer];
    line1.fillColor = UIColorFromRGB(0xEBEBEB).CGColor;
    UIBezierPath *path1 = [UIBezierPath bezierPathWithRect:CGRectMake((center - 100)/2.0, 20, width, 1)];
    line1.path = path1.CGPath;
    [self.tableView.mj_footer.layer addSublayer:line1];
    
    CAShapeLayer *line2 = [CAShapeLayer layer];
    line2.fillColor = UIColorFromRGB(0xEBEBEB).CGColor;
    UIBezierPath *path2 = [UIBezierPath bezierPathWithRect:CGRectMake((center - 100)/2.0 + 110 + width, 20, width, 1)];
    line2.path = path2.CGPath;
    [self.tableView.mj_footer.layer addSublayer:line2];
}


- (void)loadNoData
{
    [self endLoadMore];
    [self.reloadLab setText:@"暂无数据"];
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"meat_tranfer_blank"] forState:UIControlStateNormal];
    [self noData];
}

- (void)loadErrorData
{
    [self endLoadMore];
    [self.reloadLab setText:@"咦？断网了"];
    [_reloadBtn setBackgroundImage:[UIImage imageNamed:@"meat_nomal_blank"] forState:UIControlStateNormal];

    [self noData];
}

- (void)haveData{
    
    self.tableView.hidden = NO;
    self.reloadLab.hidden = YES;
    self.reloadBtn.hidden = YES;
}

- (void)noData{
    self.tableView.hidden = YES;
    self.reloadLab.hidden = NO;
    self.reloadBtn.hidden = NO;
}

@end
